package com.kedll.library.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.kedll.library.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by QiQi on 2016/10/3.
 */
public abstract class MyExpandableListAdapter<T> extends BaseExpandableListAdapter {
    protected LayoutInflater inflater;
    protected List<ExpandListEntity<T>> entities;
    protected Context mContext;

    public MyExpandableListAdapter(Context context, List<ExpandListEntity<T>> entities) {
        this.entities = entities;
        mContext = context;
        inflater = LayoutInflater.from(context);
    }


    public void add(ExpandListEntity<T> data) {
        if (entities == null) {
            entities = new ArrayList<>();
        }

        entities.add(data);
        notifyDataSetChanged();
    }

    public void addAll(List<ExpandListEntity<T>> datas) {
        if (entities == null) {
            entities = new ArrayList<>();
        }

        entities.addAll(datas);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (entities == null) {
            return;
        }

        if (position > entities.size()) {
            return;
        }

        entities.remove(position);
        notifyDataSetChanged();
    }

    public void remove(ExpandListEntity<T> data) {
        if (entities == null) {
            return;
        }

        entities.remove(data);
        notifyDataSetChanged();
    }

    public void clear() {
        if (entities == null) {
            return;
        }

        entities.clear();
        notifyDataSetChanged();
    }

    public ExpandListEntity<T> getChildValue(int groupPosition) {
        return CollectionUtils.get(entities, groupPosition);
    }

    /**
     * 查询组的索引
     *
     * @param s
     * @return
     */
    public int indexOf(String s) {
        for (int i = 0; i < getGroupCount(); i++) {
            if (entities.get(i).getHead().equals(s)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getGroupCount() {
        return CollectionUtils.size(entities);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (entities == null) {
            return 0;
        }
        return CollectionUtils.size(entities.get(groupPosition).getSubList());
    }

    @Override
    public String getGroup(int groupPosition) {
        if (entities == null) {
            return "";
        }
        return Objects.requireNonNull(CollectionUtils.get(entities, groupPosition)).getHead();
    }

    @Override
    public T getChild(int groupPosition, int childPosition) {
        List<T> subList = Objects.requireNonNull(CollectionUtils.get(entities, groupPosition)).getSubList();
        return CollectionUtils.get(subList, childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(com.kedll.library.R.layout.expandable_listview_group, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String groupText = getGroup(groupPosition);
        holder.group.setText(groupText);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    protected static class ViewHolder {
        public TextView group;

        public ViewHolder(View view) {
            group = view.findViewById(com.kedll.library.R.id.group);
        }
    }
}
